package com.xiaohai.wx.weather.message;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xiaohai.wx.weather.dto.Weather;
import com.xiaohai.wx.weather.dto.User;
import com.xiaohai.wx.weather.service.Constants;
import com.xiaohai.wx.weather.service.LocationService;
import com.xiaohai.wx.weather.service.UserService;
import com.xiaohai.wx.weather.service.WeatherService;
import com.xiaohai.wx.weather.util.MessageUtil;

@Component("TextMsgHandler")
public class TextMsgHandler {
	
	@Autowired
	LocationService locationService;
	@Autowired
	WeatherService weatherService;
	@Autowired
	UserService userService;
	
	@Autowired
	SessionHandler sessionHandler;	
	
	private final String locationError = "对不起，您输入的地区未能被识别或尚未收录。";
	private final String getWeatherError = "对不起，可能由于网络原因，未能获取到 XX 天气数据。";
	
	public String handlerMessage(String msgId, String msg, String openId){
	
		String topic = sessionHandler.getTopic(openId);
		
		if(SessionHandler.TOPIC_QUERY_LOCATION.equals(topic)){
			String response = handleSubscription(msgId, msg, openId);
			sessionHandler.removeSession(openId);
			return response;
			
		}else{
			
			if(msg.contains("取消订阅")){
				String response = this.handleUnsubscription(openId);
				return response;
			}
			
			if(msg.contains("订阅")){
				String response = handleSubscription(msgId, msg, openId);
				return response;
			}
			
			List<String> locationList= locationService.getLocationFromStr(msg); 
			if(locationList == null || locationList.isEmpty()){
				return locationError + Constants.HELP_INFO;
			}
			
			StringBuilder sb = new StringBuilder(); 
    		
    		for(String cityName : locationList){
    			
    			List<Weather> weatherList = weatherService.getWeather(cityName);
    			if(weatherList == null || weatherList.isEmpty()){
    				
    			}else{
    				sb.append(MessageUtil.get3DayWeatherStr(weatherList, cityName));
    				sb.append("\n\n");
    			}
    		}
    		
    		if(sb.length() == 0){
    			return getWeatherError.replace("XX", msg);
    		}else{
    			return sb.substring(0, sb.length()-2);
    		}

		}
	}
	
	private String handleUnsubscription(String openId){
		User user = userService.getUserFromDB(openId);
		if(user == null){
			user = new User();
			user.setOpenId(openId);
			user.setGuanzhuTime(new Date());
		}
		user.setLocation("");
		userService.addOrReplaceConsumerToDB(user);

		String response = "取消订阅成功。\n"+ Constants.HELP_INFO;
		return response;
	}
	
	
	private String handleSubscription(String msgId, String msg, String openId){
		
		String response = "";
		
		List<String> cityList = locationService.getLocationFromStr(msg);   
		if(cityList == null || cityList.isEmpty()){
			response = locationError + Constants.HELP_INFO;
		}else{
			StringBuilder city = new StringBuilder();
			StringBuilder cityNames = new StringBuilder();
			for(String cityName : cityList){
				city.append(", "+ cityName);
				cityNames.append("," + cityName);
			}
			
			User user = userService.getUserFromDB(openId);
			if(user == null){
				user = new User();
				user.setOpenId(openId);
				user.setGuanzhuTime(new Date());
			}
			user.setLocation(cityNames.substring(1));
			userService.addOrReplaceConsumerToDB(user);

			response = "订阅成功，识别到地区："+city.substring(2) +"。\n "+ Constants.HELP_INFO;
		}
		
		return response;
	}
	
}
